package com.haizhi.crm.search.service.impl;

import com.haizhi.crm.common.constant.Keys;
import com.haizhi.crm.search.builder.SearchFactory;
import com.haizhi.crm.search.builder.SearchQoBuilder;
import com.haizhi.crm.search.constant.Company;
import com.haizhi.crm.search.manager.SearchManager;
import com.haizhi.crm.search.model.SearchQo;
import com.haizhi.crm.search.model.SearchResp;
import com.haizhi.crm.search.model.SearchVo;
import com.haizhi.crm.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by chengmo on 2018/5/16.
 */
@Service
public class SearchServiceImpl implements SearchService {

    @Autowired
    private SearchManager searchManager;

    @Override
    public SearchVo search(SearchQo searchQo) {
        SearchResp searchResp = searchManager.search(searchQo);
        return SearchVo.build(searchResp);
    }

    @Override
    public SearchVo suggest(SearchQo searchQo) {
        SearchQoBuilder builder = SearchFactory.searchQoBuilder();
        builder.setGraph(searchQo.getGraph());
        builder.setKeyword(searchQo.getKeyword());
        builder.setPageSize(searchQo.getPageSize());
        builder.addSchemas(Company.SCHEMA);
        builder.addMatchQuery(Company.NAME_FIELD);
        builder.addHitFields(Company.NAME_FIELD, Keys.OBJECT_KEY);
        SearchResp searchResp = searchManager.search(builder.get());
        return SearchVo.build(searchResp);
    }
}
